package com.hnkywl.sts.action.jhgl;

import com.hnkywl.sts.entity.jhgl.Bgfkjh;
import com.hnkywl.sts.entity.organ.Nbbm;
import com.hnkywl.sts.entity.organ.Nbgs;
import com.hnkywl.sts.entity.organ.Qyjg;
import com.hnkywl.sts.entity.sfgl.Sfk;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.service.jhgl.BgfkjhService;
import com.hnkywl.sts.service.organ.GsjgService;
import com.hnkywl.sts.service.organ.NbbmService;
import com.hnkywl.sts.service.organ.NbgsService;
import com.hnkywl.sts.service.organ.QyjgService;
import com.hnkywl.sts.service.sfgl.FkshService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.POIUtil;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 13-6-26
 * Time: 下午3:24
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/jhgl/bgfkjh")
public class BgfkjhAction extends SimpleActionSupport {
    private BgfkjhService bgfkjhService;
    private NbbmService nbbmService;
    private NbgsService nbgsService;
    private QyjgService qyjgService;
    private Integer nf;
    private Integer yf;
    private Long gsId;
    private Long bmId;
    private Long gysId;
    private Bgfkjh bgfkjh;
    private Sfk bgfk;
    private Yg yg;
    private Long zggsId;
    private Long bgfkId;
    private FkshService fkshService;
    private GsjgService gsjgService;
    private String bgfkyf;
    private int page;
    private int rows;

    @Action("bgfkjh")
    public String bgfkjh() {
        nf = Calendar.getInstance().get(Calendar.YEAR);
        yf = Calendar.getInstance().get(Calendar.MONTH) + 1;
        gsId = YgService.getCurrentYgZgGsId();
        return SUCCESS;
    }

    @Action("bgfkjh_fksq")
    public String bgfkjhFksq() {
        //进入update页面
        yg = YgService.getCurrentYg();
        zggsId = YgService.getCurrentYgZgGsId();
        Long zgbmId = yg.getZgjg().getId();
        if (bgfkId != null) {
            bgfk = fkshService.findFkshDetail(bgfkId);
        } else {
            bgfk = new Sfk();
            bgfk.setSbr(yg);
            bgfk.setKhgs((Nbgs) gsjgService.findGsjgDetail(gsId));
            bgfk.setSbgs((Nbgs) gsjgService.findGsjgDetail(zggsId));
            bgfk.setSbbm((Nbbm) gsjgService.findGsjgDetail(zgbmId));
            bgfk.setGldw(qyjgService.findQyjgDetail(gysId));
            bgfk.setSfje(Double.parseDouble(bgfkyf.replace("_", ".")));
        }
        return SUCCESS;
    }


    @Action("bgfkjhList")
    public void bgfkjhList() {
//        List<Nbgs> roots = nbgsService.findAllRootNbgs();
//        if (!CollectionUtils.isEmpty(roots))
//            gsId = roots.get(0).getId();
        renderJson(bgfkjhService.bgfkjhList(nf, yf, gsId, gysId, page, rows));
    }

    @Action("hbzf")
    public String hbzf() {
        return SUCCESS;
    }

    @Action("hbzfList")
    public void hbzfList() {
        renderJson(bgfkjhService.bgfkjhList(gsId, gysId, 1));
    }

    @Action("bgfkjh_hbzfcontent")
    public String hbzfContent() {
        bgfkjh = new Bgfkjh();
        bgfkjh.setNbgs(nbgsService.findNbgsDetail(gsId));
        bgfkjh.setGys(qyjgService.findQyjgDetail(gysId));
        return SUCCESS;
    }

    @Action("saveHbzf")
    public void saveHbzf() {
        try {
            bgfkjhService.saveHbzf(bgfkjh);
            renderText("{\"statusCode\":200}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":300,\"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("hbcx")
    public String hbcx() {
        return SUCCESS;
    }

    @Action("hbcxList")
    public void hbcxList() {
        renderJson(bgfkjhService.bgfkjhList(gsId, gysId, 2));
    }

    @Action("bgfkjh_hbcxcontent")
    public String hbcxContent() {
        bgfkjh = new Bgfkjh();
        bgfkjh.setBgmyb(nbbmService.findNbbmDetail(bmId));
        bgfkjh.setGys(qyjgService.findQyjgDetail(gysId));
        return SUCCESS;
    }

    @Action("saveHbcx")
    public void saveCxzf() {
        try {
            Nbbm bgmyb = nbbmService.findNbbmDetail(bgfkjh.getBgmyb().getId());
            Nbbm zcbm = nbbmService.findNbbmDetail(bgfkjh.getZcbm().getId());
            Nbbm zrbm = nbbmService.findNbbmDetail(bgfkjh.getZrbm().getId());
            bgfkjh.setNbgs(bgmyb.getSjjg());
            bgfkjh.setZcgs(zcbm.getSjjg());
            bgfkjh.setZrgs(zrbm.getSjjg());
            bgfkjhService.saveCx(bgfkjh);
            renderText("{\"statusCode\":200}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":300,\"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("export")
    public String export() {
//        List<Nbgs> roots = nbgsService.findAllRootNbgs();
//        if (!CollectionUtils.isEmpty(roots))
//            gsId = roots.get(0).getId();
        Map<String, Map> map = bgfkjhService.bgfkjhListExport(nf, yf, gsId, gysId, 1, 1000000);
        List<Map> result = (List<Map>) map.get("rows");
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("sheet1");
        POIUtil poiUtil = new POIUtil(wb);
        sheet.setDefaultRowHeightInPoints(21);
        sheet.setDefaultColumnWidth((short) 20);
        HSSFCellStyle headerStyle = poiUtil.createHeader3HSSFCellStyle(wb);
        poiUtil.fillRow(sheet, headerStyle, 0, "供应商", "期初总余额", "货币支付", "本月应付5", "本月应付4", "本月应付1", "本月其他应付",
                "上期未付", "本期实付", "本期应付", "期末总余额");
        for (int i = 1; i < result.size() + 1; i++) {
            Map item = result.get(i - 1);
            poiUtil.fillRowWithScale(sheet, null, i, 2, item.get("gys"), item.get("qc"), item.get("hbzf"), item.get("yf5"), item.get("yf4"),
                    item.get("yf1"), item.get("yfqt"), item.get("sqwf"), item.get("bqsf"), item.get("yf"), item.get("qm"));
        }
//        HSSFCellStyle sumStyle = wb.createCellStyle();
//        HSSFFont font = wb.createFont();
//        font.setColor(HSSFFont.COLOR_RED);
//        sumStyle.setFont(font);
//        poiUtil.fillRowWithScale(sheet,sumStyle,result.size() + 1,2,"合计：","","","","","","","","","","",
//                map.get("sum"),"");
        try {
            response.setContentType("application/x-download");
            String filedisplay = new String(("保供付款计划.xls").getBytes("gb2312"), "iso8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            wb.write(response.getOutputStream());
            response.getOutputStream().close();
        } catch (Exception e) {
            e.printStackTrace();
            renderText("位置异常，导出失败" + e.getMessage());
        }
        return null;
    }

    public Integer getNf() {
        return nf;
    }

    public void setNf(Integer nf) {
        this.nf = nf;
    }

    public Integer getYf() {
        return yf;
    }

    public void setYf(Integer yf) {
        this.yf = yf;
    }

    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }

    public Long getBmId() {
        return bmId;
    }

    public void setBmId(Long bmId) {
        this.bmId = bmId;
    }

    public Long getGysId() {
        return gysId;
    }

    public void setGysId(Long gysId) {
        this.gysId = gysId;
    }

    public Bgfkjh getBgfkjh() {
        return bgfkjh;
    }

    public void setBgfkjh(Bgfkjh bgfkjh) {
        this.bgfkjh = bgfkjh;
    }

    public Sfk getBgfk() {
        return bgfk;
    }

    public void setBgfk(Sfk bgfk) {
        this.bgfk = bgfk;
    }

    public Yg getYg() {
        return yg;
    }

    public void setYg(Yg yg) {
        this.yg = yg;
    }

    public Long getZggsId() {
        return zggsId;
    }

    public void setZggsId(Long zggsId) {
        this.zggsId = zggsId;
    }

    public Long getBgfkId() {
        return bgfkId;
    }

    public void setBgfkId(Long bgfkId) {
        this.bgfkId = bgfkId;
    }

    public String getBgfkyf() {
        return bgfkyf;
    }

    public void setBgfkyf(String bgfkyf) {
        this.bgfkyf = bgfkyf;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    @Autowired
    public void setBgfkjhService(BgfkjhService bgfkjhService) {
        this.bgfkjhService = bgfkjhService;
    }

    @Autowired
    public void setNbbmService(NbbmService nbbmService) {
        this.nbbmService = nbbmService;
    }

    @Autowired
    public void setNbgsService(NbgsService nbgsService) {
        this.nbgsService = nbgsService;
    }

    @Autowired
    public void setQyjgService(QyjgService qyjgService) {
        this.qyjgService = qyjgService;
    }

    @Autowired
    public void setFkshService(FkshService fkshService) {
        this.fkshService = fkshService;
    }

    @Autowired
    public void setGsjgService(GsjgService gsjgService) {
        this.gsjgService = gsjgService;
    }
}
